//JS
var app = getApp()
let orderStatus = 1; //0"新订单，未支付;1"新订单，已支付";2, "已取消"；3"待评价"；4“已完成”
Page({
 data: {
  // 顶部菜单切换
  navbar: ["待上餐", "已取消", "已完成", "已过期"],
  // 默认选中菜单
  currentTab: 0,
  isShowComment: false, //是否显示评论框
  list: [],
  comment: '',
  starDesc: '非常满意，无可挑剔',
  commentStatus: 4,
  stars: [{
   lightImg: '../../image/startlight.png',
   blackImg: '../../image/startgrey.png',
   flag: 1,
   message: '非常不满意，各方面都很差'
  }, {
    lightImg: '../../image/startlight.png',
    blackImg: '../../image/startgrey.png',
   flag: 1,
   message: '不满意，比较差'
  }, {
    lightImg: '../../image/startlight.png',
    blackImg: '../../image/startgrey.png',
   flag: 1,
   message: '一般，还要改善'
  }, {
    lightImg: '../../image/startlight.png',
    blackImg: '../../image/startgrey.png',
   flag: 1,
   message: '比较满意，仍要改善'
  }, {
    lightImg: '../../image/startlight.png',
    blackImg: '../../image/startgrey.png',
   flag: 1,
   message: '非常完美，无可挑剔'
  }]
 },

 // 选择评价星星
 starClick: function(e) {
  var that = this;
  for (var i = 0; i < that.data.stars.length; i++) {
   var allItem = 'stars[' + i + '].flag';
   that.setData({
    [allItem]: 2
   })
  }
  var index = e.currentTarget.dataset.index;
  for (var i = 0; i <= index; i++) {
   var item = 'stars[' + i + '].flag';
   that.setData({
    [item]: 1,
    commentStatus: index
   })
  }
  this.setData({
   starDesc: this.data.stars[index].message,
  })
 },

 // 初始化星星和评论
 starClickInit: function(e) {
  var that = this;
  for (var i = 0; i < that.data.stars.length; i++) {
   var allItem = 'stars[' + i + '].flag';
   that.setData({
    [allItem]: 2
   })
  }
  var index = 4;
  for (var i = 0; i <= index; i++) {
   var item = 'stars[' + i + '].flag';
   that.setData({
    [item]: 1,
    commentStatus: index
   })
  }
  this.setData({
   starDesc: this.data.stars[index].message,
   comment: ''
  })
 },
 
 //顶部tab切换
 navbarTap: function(e) {
  let index = e.currentTarget.dataset.idx;
  this.setData({
   currentTab: index
  })

  //1"新订单，已支付";2, "已取消"；3"待评价"；4“已完成”
  if (index == 1) {
   orderStatus = 1;
  } else if (index == 2) {
   orderStatus = 2;
  } else if (index == 3) {
   orderStatus = 3;
  } else {
   orderStatus = 1;
  }
  this.getMyOrderList();
 },
 onLoad: function(options) {
  var wx_user =  wx.getStorageSync('wx_user');
  if (wx_user == null || wx_user == '') {
    wx.showToast({
      title: '登录信息过期，请登录！',
      icon: 'none',
      duration: 1000,
      success:function(){
        setTimeout(function () {
          //要延时执行的代码,延迟1s
          wx.navigateTo({
            url: '../login/login',
          })
        },1000)
      }
    })   
  }
  this.getMyOrderList();
 },

//  onShow: function() {
  
//  },

 getMyOrderList() {
  let that = this;
  // let openid = app._checkOpenid();
  var wx_user =  wx.getStorageSync('wx_user');
  var iphone = wx_user.iphone;
  // 获取订单状态
  var orderStatus = that.data.currentTab;
  var payStatus = 0;
  // 根据订单状态设置支付状态,判断是否是取消订单，若是取消则设置支付状态值为"1"
  if (orderStatus == 1) {
    payStatus = 1;
  }
  // if (!openid) {
  //  return;
  // }
  //请求自己后台
  wx.request({
   url: app.globalData.baseUrl + '/wechatprogram/ordermaster/listOrder',
   method: 'post',
   header: {
     'content-type': 'application/x-www-form-urlencoded',
   },
   data: {
    buyerPhone: iphone,
    orderStatus: orderStatus,
    payStatus: payStatus
   },
   success: function(res) {
    console.log(res);
    if (res && res.data && res.data.rows && res.data.rows.length > 0) {
     let dataList = res.data.rows;
     console.log(dataList)
     that.setData({
      list: dataList
     })
    } else {
     that.setData({
      list: []
     })
    }
   }
  })
 },
 //去评论页面列表
 goCommentPage() {
  wx.navigateTo({
   url: '../mycomment/mycomment',
  })
 },
 //弹起评论框
 goComment(event) {
  let orderId = event.currentTarget.dataset.orderid;
  this.setData({
   isShowComment: true,
   orderId: orderId
  })
  // let that = this;
  // let orderId = event.currentTarget.dataset.orderid;
  // wx.navigateTo({
  //   url: '../gocomment/gocomment' + orderId,
  //  })
 },
 //隐藏评论框
 cancelComment() {
  this.setData({
   isShowComment: false
  })
 },
 //获取评论内容
 setValue(input) {
  this.setData({
   comment: input.detail.value
  })
 },
 //提交评论
 submitComment() {
  let that = this;
  that.cancelComment();
  let content = that.data.comment;
  let orderId = that.data.orderId;
  let commentStatus = that.data.commentStatus;
  if (!content) {
   wx.showToast({
    title: '评论内容为空',
   })
   return;
  }
  // 获取用户名和电话号码
  var buyer = wx.getStorageSync('wx_user');
  // 判断用户是否在内存中存在，若不存在，提示用户登录信息过期，请登录
  if (buyer == null || buyer == '') {
    wx.showToast({
      title: '登录信息过期，请登录！',
      icon: 'none',
      duration: 1000,
      success:function(){
        setTimeout(function () {
          //要延时执行的代码,延迟1s
          wx.navigateTo({
            url: '../login/login',
          })
        },1000)
      }
    })
  }
  var buyerName = buyer.name;
  var buyerPhone = buyer.iphone;
  wx.request({
   url: app.globalData.baseUrl + '/wechatprogram/comment/add',
   method: "POST",
   header: {
    "Content-Type": "application/x-www-form-urlencoded"
   },
   data: {
    wxOrderId: orderId,
    name: buyerName,
    phone: buyerPhone,
    content: content,
    commentStatus: commentStatus
   },
   success: function(res) {
    that.getMyOrderList();
    that.starClickInit();
    // 这里修改成跳转的页面
    if (res.data.code == 0) {
      wx.showToast({
        title: '评论成功！',
        icon: 'success',
        duration: 1000
        })   
      } else {
        wx.showToast({   
          title: res.data.msg,   
          icon: 'none',   
          duration: 2000   
        })
      } 
    },
    fail: (res) => {
      wx.showToast({
        title: '评论失败！',   
        icon: 'none',   
        duration: 2000
      })
    }
  })
 },


 //催单
 clickSure(event) {
  let that = this;
  let orderId = event.currentTarget.dataset.orderid;
  wx.showToast({
   title: '催单成功',
  })
 },
 clickGoPay(event) {
  let that = this;
  let orderId = event.currentTarget.dataset.orderid;
  wx.showModal({
   title: '模拟支付',
   content: '确定去支付吗？',
   success(res) {
    if (res.confirm) {
     that.goPay(orderId)
    } else if (res.cancel) {
     console.log('用户点击取消')
    }
   }
  })
 },
 //去支付
 goPay(orderId) {
  let that = this;
  let openid = app._checkOpenid();
  if (!openid) {
   return;
  }

  //请求自己后台获取用户openid
  wx.request({
   url: app.globalData.baseUrl + '/pay/goPay',
   data: {
    orderId: orderId
   },
   success: function(res) {
    console.log(res);
    if (res && res.data && res.data.data) {
     let dataList = res.data.data;
     that.getMyOrderList();
     wx.showToast({
      title: '支付成功',
     })
    } else {
     wx.showToast({
      title: '支付失败',
     })
    }
   }
  })
 },
 //取消订单
 cancleOrder(event) {
  let that = this;
  let orderId = event.currentTarget.dataset.orderid;
  
  // 取消订单唯一标识
  var orderStatus = 1;
  // 支付状态
  var payStatus = 1;
  wx.request({
   url: app.globalData.baseUrl + '/wechatprogram/ordermaster/edit',
   method: "POST",
   header: {
    "Content-Type": "application/x-www-form-urlencoded"
   },
   data: {
    id: orderId,
    orderStatus: orderStatus,
    payStatus: payStatus
   },
   success: function(res) {
    if (res.data.code == 0) {
      wx.setStorageSync('wx_user',  res.data.data);
      wx.showToast({
        title: '取消订单成功！',
        icon: 'success',
        duration: 1000
      })
    } else {
      wx.showToast({
        title: '取消订单失败！',
        icon: 'none',
        duration: 1000
      })
    }
    that.getMyOrderList();
   }
  })
 }






})